package com.itheima.demo07PreparedStatement改写登录案例;

import java.sql.*;
import java.util.Scanner;

public class Demo07 {
	public static void main(String[] args) throws SQLException {
		// 1.使用数据库保存用户的账号和密码
		// 2.让用户输入账号和密码
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入账号:");
		String name = sc.nextLine();
		System.out.println("请输入密码:");
		String password = sc.nextLine();

		// 3.使用SQL根据用户的账号和密码去数据库查询数据
		Connection conn = DriverManager.getConnection("jdbc:mysql:///day18", "root", "root");

		String sql = "SELECT * FROM user WHERE name=? AND password=?;";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, name);
		pstmt.setString(2, password);

		ResultSet rs = pstmt.executeQuery();

		if (rs.next()) {
			// 4.如果查询到数据，说明登录成功
			System.out.println("欢迎您: " + name);
		} else {
			// 5.如果查询不到数据，说明登录失败
			System.out.println("账号密码错误...");
		}

		// 6.关闭资源
		rs.close();
		pstmt.close();
		conn.close();
	}
}
